/**
 * 
 */
package de.homer.server.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import de.homer.adapter.domain.ConfiguredPlugin;
import de.homer.adapter.domain.PluginConfiguration;
import de.homer.adapter.domain.PluginMetaInformation;
import de.homer.persistence.adapter.ConfiguredPluginRepository;
import de.homer.server.util.PluginClassLoadingException;

/**
 * @author André Stannek
 * 
 */
@Service
public class PluginConfigurationServiceImpl implements
		PluginConfigurationService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3461527603948448249L;

	/**
	 * 
	 */
	@Autowired
	private ConfiguredPluginRepository repository;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.homer.server.service.PluginService#configurePlugin(de.homer.adapter
	 * .domain.PluginMetaInformation)
	 */
	@Override
	public final void configurePlugin(
			final PluginMetaInformation metaInformation)
			throws PluginClassLoadingException {

		ConfiguredPlugin newPlugin = new ConfiguredPlugin();
		newPlugin.setConfiguredPlugin(metaInformation);
		PluginConfiguration configuration = new PluginConfiguration();
		for (String attribute : metaInformation.getConfigurableAttributes()) {
			configuration.addAttribute(attribute);
		}
		newPlugin.setConfiguration(configuration);
		repository.merge(newPlugin);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.homer.server.service.PluginConfigurationService#getConfiguredPlugins()
	 */
	@Override
	public final List<ConfiguredPlugin> getConfiguredPlugins() {
		return new ArrayList<>(repository.getAll());
	}
}
